﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.GData.Client;
using WapCrawlerCore;
using WapCrawlerLib;

namespace WapCrawler.Post
{
    public partial class frmAutoPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void WapCrawlerDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView postGridView = (GridView)UcPostGridView.FindControl("PostGridView");
            Session["wap"] = int.Parse(WapCrawlerDropDownList.SelectedValue);
            postGridView.DataBind();
        }

        protected void btnPostBlogger_Click(object sender, EventArgs e)
        {
            WapDataClassesDataContext dc = new WapDataClassesDataContext();

            GridView gridViewPost = (GridView)UcPostGridView.FindControl("PostGridView");
            string topic = txtLabel.Text;
            //Nếu chưa nhập account thì thoát ra
            if ("" == txtUsername.Text)
                return;

            Service service = new Service("blogger", "blogger-example");
            // ClientLogin using username/password authentication
            service.Credentials = new GDataCredentials(txtUsername.Text, txtPassword.Text);
            BloggerAPI objBlog = new BloggerAPI();
            Uri blogPostUri = objBlog.SelectUserBlog(service);
            blogPostUri = new Uri(blogPostUri.AbsoluteUri + "?redirect=false&max-results=200");
            SortedDictionary<string, AtomEntry> listBlogEntries = objBlog.ListBlogEntries(service, blogPostUri, "");

            foreach (GridViewRow row in gridViewPost.Rows)
            {
                CheckBox chkBx = (CheckBox)row.FindControl("chkSelect");
                if (chkBx != null && chkBx.Checked)
                {
                    WapCrawlerLib.Post objPost = dc.Posts.Single(p => p.postId == (int)gridViewPost.DataKeys[row.RowIndex].Value);
                    //Nếu không tồn tại post có dạng postTitle thì new entry
                    if (!listBlogEntries.Keys.Contains(objPost.title))
                    {
                        objBlog.PostNewEntry(service, blogPostUri, objPost, topic);
                    }
                    else //Nếu tồn tại post thì update entry
                    {
                        //Lấy entry có title chứa postTitle
                        var query = listBlogEntries
                            .Select(x => x.Value)
                            .Where(q => q.Title.Text.Contains(objPost.title));

                        foreach (AtomEntry item in query)
                            objBlog.EditEntry(item, objPost, topic);
                    }

                }
            }
        }

        protected void btnFilterPost_Click(object sender, EventArgs e)
        {
            Session["wapCrawler"] = int.Parse(WapCrawlerDropDownList.SelectedValue);
            Session["topicCrawler"] = int.Parse(TopicCrawlerDropDownList.SelectedValue);
            GridView postGridView = (GridView)UcPostGridView.FindControl("PostGridView");
            postGridView.DataBind();
        }

        protected void TopicCrawlerDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtLabel.Text = TopicCrawlerDropDownList.SelectedItem.Text;
        }

        protected void dropDownListNumberOfRows_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView postGridView = (GridView)UcPostGridView.FindControl("PostGridView");
            if (dropDownListNumberOfRows.SelectedIndex != (dropDownListNumberOfRows.Items.Count - 1))
            {
                postGridView.AllowPaging = true;
                postGridView.PageSize = int.Parse(dropDownListNumberOfRows.SelectedItem.Value);
            }
            else
            {
                postGridView.AllowPaging = false;
            }
        }

        
    }
}